package com.example.task1.ui.AllService.movies;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.MoviesOrderListBean;
import com.example.task1.http.Api;
import com.example.task1.ui.BaseAdapter;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

public class MoviesOrderListAdapter extends BaseAdapter<MoviesOrderListBean.RowsDTO> {

    private Context context;
    private List<MoviesOrderListBean.RowsDTO> mData = new ArrayList<>();
    private TextView tvTitle;
    private TextView tvTheatreName;
    private TextView tvRoomName;
    private TextView tvStartTime;
    private TextView tvEndTime;
    private Button btnDel;

    private OnSelectItem onSelectItem;
    private TextView tvLog;

    public interface OnSelectItem {
        void Item(MoviesOrderListBean.RowsDTO date, int position, myHolder holder);
    }

    public void setOnSelectItem(OnSelectItem onSelectItem) {
        this.onSelectItem = onSelectItem;
    }

    @Override
    protected void setData(List<MoviesOrderListBean.RowsDTO> rows) {
        int size = mData.size();
        mData.addAll(rows);
        notifyItemInserted(size);
    }

    @Override
    protected int getLayout() {
        return R.layout.moviesorderlist;
    }

    @Override
    protected Context getContext() {
        return context;
    }

    @Override
    protected List<MoviesOrderListBean.RowsDTO> getData() {
        return mData;
    }

    @Override
    protected void onBind(myHolder holder, MoviesOrderListBean.RowsDTO date) {

    }

    @Override
    protected void onBind(myHolder holder, MoviesOrderListBean.RowsDTO date, int position) {
        tvTitle.setText(date.getMovieName());
        tvRoomName.setText(date.getRoomName());
        if ("Y".equals(date.getStatus())) {
            tvLog.setVisibility(View.VISIBLE);
        }
        tvTheatreName.setText("地点：" + date.getTheatreName());
        tvStartTime.setText("开始时间：" + date.getStartTime());
        tvEndTime.setText("结束时间：" + date.getEndTime());

        btnDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Call<ApiResp> del = Api.api().del(MyApplication.TOKEN, "/prod-api/api/movie/ticket/order/" + date.getId());
                Api.send(del, ApiResp.class, new CallBack<ApiResp>() {
                    @Override
                    public void success(ApiResp dat) {
                        mData.remove(position);
                        notifyItemRemoved(position);
                        MyApplication.show("删除成功");
                    }

                    @Override
                    public void error(String msg) {

                    }
                });
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("N".equals(date.getStatus())) {
                    onSelectItem.Item(date, position, holder);
                }
            }
        });
    }

    @Override
    protected void holder(View itemView) {
        initView(itemView);
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private void initView(View itemView) {
        tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
        tvTheatreName = (TextView) itemView.findViewById(R.id.tv_theatreName);
        tvRoomName = (TextView) itemView.findViewById(R.id.tv_roomName);
        tvStartTime = (TextView) itemView.findViewById(R.id.tv_startTime);
        tvEndTime = (TextView) itemView.findViewById(R.id.tv_endTime);
        btnDel = (Button) itemView.findViewById(R.id.btn_del);
        tvLog = (TextView) itemView.findViewById(R.id.tv_log);
    }
}
